#!/bin/bash

set -eo pipefail

VENV_DIR=".venv"

echo "[0/6] Setting up Python virtual environment..."

if [ ! -d "$VENV_DIR" ]; then
    echo "Virtual environment not found. Creating one..."
    python3 -m venv "$VENV_DIR"
    echo "Virtual environment created."

    source "$VENV_DIR/bin/activate"

    echo "Upgrading pip and installing Conan..."

    pip install --upgrade pip
    pip install conan
else
    source "$VENV_DIR/bin/activate"
    echo "Virtual environment activated."
fi

echo "[1/6] Detecting system profile..."
conan profile detect --force

echo "[2/6] Installing dependencies..."
mkdir -p build
conan install . --output-folder=build --build=missing --deployer=direct_deploy

echo "[3/6] Entering build directory..."
cd build

echo "[4/6] Checking compiler and selecting CMake preset..."

if conan profile show | grep -q "compiler=msvc"; then
    echo "MSVC compiler detected. Using 'conan-default' preset."
    cmake .. --preset conan-default
else
    echo "Non-MSVC compiler detected. Using 'conan-release' preset."
    cmake .. --preset conan-release
fi

echo "[5/6] Building the project..."
cmake --build . --config Release

echo "[6/6] Build finished."